﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using Model;

namespace BusinessLogic
{
    public class GetACMTXCommand : BaseCommand<List<acm_tx>>
    {

        public GetACMTXCommand(int id)
        {

            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT [id],[sales_id],[vin],[campaign_id_adv],[campaign_id_dlv],");
            sb.Append("[car_series_id],[adv_date],[dlv_date],[adv_promotion_amount],[dlv_promotion_amount],");
            sb.Append("[diff_promotion_amount],[attach1],[tx_status],[tx_date],[attach1_date],");
            sb.Append("[attach1_view_date],[tx_dealer_code],[tx_branch_code],[change_from_dealer_code],[change_from_branch_code],");
            sb.Append("[change_request_status],[change_request_date],[change_reply_date],[change_reply_by],[warranty_print_date],");
            sb.Append("[warran_print_by],[addr_no],[village],[group_no],[alley],");
            sb.Append("[building_name],[room_no],[floor_no],[road_name],[parish],");
            sb.Append("[district],[province],[postcode],[customer_name],[id_card],[mobile_note4],[passport], [campaign_with_note4]");
            sb.Append(" FROM acm_tx WHERE id=@id");

            SetQuery(sb.ToString());

            cmd.Parameters.AddWithValue("id", id);
        }

        public GetACMTXCommand(string vin)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT [id],[sales_id],[vin],[campaign_id_adv],[campaign_id_dlv],");
            sb.Append("[car_series_id],[adv_date],[dlv_date],[adv_promotion_amount],[dlv_promotion_amount],");
            sb.Append("[diff_promotion_amount],[attach1],[tx_status],[tx_date],[attach1_date],");
            sb.Append("[attach1_view_date],[tx_dealer_code],[tx_branch_code],[change_from_dealer_code],[change_from_branch_code],");
            sb.Append("[change_request_status],[change_request_date],[change_reply_date],[change_reply_by],[warranty_print_date],");
            sb.Append("[warran_print_by],[addr_no],[village],[group_no],[alley],");
            sb.Append("[building_name],[room_no],[floor_no],[road_name],[parish],");
            sb.Append("[district],[province],[postcode],[customer_name],[id_card],[mobile_note4],[passport], [campaign_with_note4]");
            sb.Append(" FROM acm_tx WHERE vin=@vin");

            SetQuery(sb.ToString());

            cmd.Parameters.AddWithValue("vin", vin);
        }

        public override void Command()
        {
            Result = new List<acm_tx>();
            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                acm_tx row = new acm_tx();

                row.id = dr.GetInt32(0);
                row.sales_id = GetNullableInt(dr,1);
                row.vin  = GetNullableString(dr,2); 
                row.campaign_id_adv  = GetNullableInt(dr,3); 
                row.campaign_id_dlv  = GetNullableInt(dr,4); 
                row.car_series_id  = GetNullableInt(dr,5); 
                row.adv_date  = GetNullableDateTime(dr,6);
                row.dlv_date = GetNullableDateTime(dr, 7); 
                row.adv_promotion_amount  = Convert.ToDecimal(GetNullableDouble(dr,8));
                row.dlv_promotion_amount = Convert.ToDecimal(GetNullableDouble(dr, 9));
                row.diff_promotion_amount = Convert.ToDecimal(GetNullableDouble(dr, 10)); 
                row.attach1  = GetNullableString(dr,11);
                row.tx_status = GetNullableString(dr, 12);
                row.tx_date = GetNullableDateTime(dr, 13);
                row.attach1_date = GetNullableDateTime(dr, 14);
                row.attach1_view_date = GetNullableDateTime(dr, 15);
                row.tx_dealer_code = GetNullableString(dr, 16);
                row.tx_branch_code = GetNullableString(dr, 17);
                row.change_from_dealer_code = GetNullableString(dr, 18);
                row.change_from_branch_code = GetNullableString(dr, 19);
                row.change_request_status = GetNullableString(dr, 20);
                row.change_request_date = GetNullableDateTime(dr, 21);
                row.change_reply_date = GetNullableDateTime(dr, 22); 
                row.change_reply_by  = GetNullableInt(dr,23);
                row.warranty_print_date = GetNullableDateTime(dr, 24); 
                row.warran_print_by  = GetNullableInt(dr,25);
                row.addr_no = GetNullableString(dr, 26);
                row.village = GetNullableString(dr, 27);
                row.group_no = GetNullableString(dr, 28);
                row.alley = GetNullableString(dr, 29);
                row.building_name = GetNullableString(dr, 30);
                row.room_no = GetNullableString(dr, 31);
                row.floor_no = GetNullableString(dr, 32);
                row.road_name = GetNullableString(dr, 33);
                row.parish = GetNullableString(dr, 34);
                row.district = GetNullableString(dr, 35);
                row.province = GetNullableString(dr, 36);
                row.postcode = GetNullableString(dr, 37);
                row.customer_name = GetNullableString(dr, 38);
                row.id_card = GetNullableString(dr, 39);
                row.mobile_note4 = GetNullableInt(dr, 40);
                row.passport = GetNullableBoolean(dr,41);
                row.campaign_with_note4 = GetNullableInt(dr, 42);
                Result.Add(row);
            }
        }
    }
}
